<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inputValue"
      @change="handleInputChange($event)"
      ref="addInput"
    />
    <a-button type="primary" @click="addList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox
          :checked="item.done"
          @change="cbStatusChange({ e: $event, id: item.id })"
          >{{ item.info }}</a-checkbox
        >
        <!-- 删除链接 -->
        <a slot="actions" @click="cleanItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ unDoneItem }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="changeClass == 'all' ? 'primary' : 'default'"
            @click="changeList('all')"
            >全部</a-button
          >
          <a-button
            :type="changeClass == 'unDone' ? 'primary' : 'default'"
            @click="changeList('unDone')"
            >未完成</a-button
          >
          <a-button
            :type="changeClass == 'Done' ? 'primary' : 'default'"
            @click="changeList('Done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  created() {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['inputValue', 'changeClass']),
    ...mapGetters(['unDoneItem', 'infoList'])
  },
  methods: {
    ...mapMutations([
      'handleInputChange',
      'cleanItem',
      'cbStatusChange',
      'clearDone',
      'changeList',
    ]),

    // 添加操作
    // addList(e) {
    //   this.$store.commit('addList')
    //   // 添加完毕后获取焦点
    //   this.$nextTick(() => {
    //     this.$refs.addInput.focus()
    //   })
    // }
  },
  watch:{
    infoList:{
      handler(newvalue){
        localStorage.setItem('List',JSON.stringify(newvalue))
      },
      immediate:true
    }
  },
  mounted() {
    console.log(this)
  },
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
